import { addCategoryAPI, deleteCategoryAPI, fetchCategoryListAPI, updateCategoryAPI, updateCategoryStatusAPI } from '@/api/categoryAPI'

const actions = {
  /* 获取所有分类的action */
  async fetchCategoryListAction(context, pageParams) {
    const { data } = await fetchCategoryListAPI(pageParams)
    context.commit('setCategoryListMutation', data)
  },

  /* 删除分类的action */
  async deleteCategoryAction(context, categoryId) {
    await deleteCategoryAPI(categoryId)
    return '删除分类成功'
  },
  /* 更新分类状态的action */
  async updateCategoryStatusAction(context, { status, id }) {
    await updateCategoryStatusAPI(status, id)
    return status === 1 ? '启用成功' : '禁用成功'
  },
  /* 添加分类的action */
  async addCategoryAction(context, category) {
    await addCategoryAPI(category)
    return category.type === '1' ? '新增菜品分类成功' : '新增套餐分类成功'
  },
  /* 更新分类的action */
  async updateCategoryAction(context, category) {
    await updateCategoryAPI(category)
    return '编辑分类成功'
  }
}

const mutations = {
  /* 设置分类列表，分类总数的mutation */
  setCategoryListMutation(state, { total, records }) {
    state.total = total
    state.records = records
  }
}

const state = {
  records: [],
  total: 0
}

export default {
  namespaced: true,
  state,
  mutations,
  actions
}
